package com.freen.fadmin.entity.sysuser;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.freen.fadmin.entity.SysBaseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Entity
@Table(name = "sys_user",schema = "sys_user")
@JsonIgnoreProperties(value = { "sys_user" })
public class SysUser extends SysBaseEntity implements UserDetails {
    @Id
    @GeneratedValue
    private Integer id;
    @Column(name = "name",length = 20,nullable = false,unique = true)
    private String name;
    @Column(name = "cnname",length = 20)
    private String cnname;
    @Column(name = "avatar",length = 80)
    private String avatar;
    @Column(name = "phone",length = 20,nullable = false,unique = true)
    private String phone;
    @Column(name = "login_ip",length = 20)
    private String loginIP;
    @Column(name = "remember_token",length = 255)
    private String rememberToken;
    @Column(name = "password",length = 255)
    private String password;
    private Boolean enabled = Boolean.TRUE;
    /**
     * 多对多映射，用户角色
     */
    @ManyToMany(cascade = {CascadeType.REFRESH},fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role",schema = "sys_user_role",joinColumns = {@JoinColumn(name = "sysuser_id",columnDefinition = "INT(11)")},inverseJoinColumns = {@JoinColumn(name = "role_id",columnDefinition = "INT(11)")})
    private List<SysRole> roles;

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setCnname(String cnname) {
        this.cnname = cnname;
    }

    public String getCnname() {
        return cnname;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void setLoginIP(String loginIP) {
        this.loginIP = loginIP;
    }

    public String getLoginIP() {
        return loginIP;
    }

    public void setRememberToken(String rememberToken) {
        this.rememberToken = rememberToken;
    }

    public String getRememberToken() {
        return rememberToken;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getEnabled() {
        return enabled;
    }
    //@JsonBackReference
    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }
    //@JsonBackReference
    public List<SysRole> getRoles() {
        return roles;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //获取当前用户实例的 password
    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    public SysUser(String name,String password){
        this.name = name;
        this.password = password;
    }

    public SysUser() {
    }

    /**
     * 根据自定义逻辑来返回用户权限，如果用户权限返回空或者和拦截路径对应权限不同，验证不通过
     */

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<SysRole> roles = this.getRoles();
        for (SysRole role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getRole()));
        }
        return authorities;
    }
    /**
     * 获取当前用户实例的name
     */
    @Override
    public String getUsername() {
        return this.name;
    }
    /**
     * 帐号是否不过期，false则验证不通过
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    /**
     * 帐号是否不锁定，false则验证不通过
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    /**
     * 凭证是否不过期，false则验证不通过
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    /**
     * 该帐号是否启用，false则验证不通过
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
